
let Command = require('./command');

// 某个chain上删除 某个 sel
class RemoveSelCommand extends Command{
    constructor(chain,sel){
        super();

        this.name = 'Remove Sel Node';
        this.type = 'RemoveSel';

        // this.chain = chain;
        // this.sel = sel;
        // this.selIdx = sel.getIdx();

        //
        this._localPos = sel.node.getPosition();

        // 依赖的对象
        // this.depens.push({
        //     name:'chain',
        //     obj:this.chain,
        // });
        // this.depens.push({
        //     name:'sel',
        //     obj:this.sel
        // });

        this.setObj('chain',chain);
        this.setObj('sel',sel);

        this._lstAffect = [];
        this._selIdx = -1;
    }

    execute(){

        let sel = this.getObj('sel');
        let chain = this.getObj('chain');
        myTools.asset(sel,'sel can not null!');
        myTools.asset(chain,'chain can not null!');

        // 记录下之前那些cmd依赖sel
        this._lstAffect = this.getDepenItems(sel);
        this._selIdx = chain.removeSel(sel);

        // 判断节点被删除后,chain是否也需要被删除
        let mapScene = G.gameMap;
        if(mapScene.curChain.lstSel.length < 1 &&
           mapScene.lstChain.length > 1){
            // 删除 chain
            let mapScene = G.gameApp;
            mapScene.removeChain(chain);
            this.setObj('chain',null);
        }

        this.cleanAffectLst(this._lstAffect);
    }

    undo(){

        let chain = this.getObj('chain');

        // 在上面已经删除了
        if(!chain){
            chain = G.gameApp._createChain();
            this.setObj('chain',chain);
        }

        let sel = chain.addSel(this._localPos);

        // 将  sel 放到 this._selIdx 的位置
        myTools.asset(this._selIdx >= 0, 'this._selIdx 应该 >= 0');

        chain.resetLastSelIdx(this._selIdx);

            // 更新
        this.setAffectLst(this._lstAffect,sel);
    }
}

module.exports = RemoveSelCommand;
